package com.guyj.baseapp.demo.zxing;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.guyj.baseapp.MainActivity;
import com.guyj.baseapp.R;
import com.guyj.baseapp.entity.DemoBean;
import com.guyj.common.utils.LogUtils;
import com.guyj.common.utils.QRCodeUtils;
import com.guyj.common.base.BaseActivity;
import com.guyj.common.utils.ImageUtils;
import com.guyj.common.utils.ToastUtil;
import com.guyj.common.views.LazyTextView;
import com.guyj.common.zxing.activity.CaptureActivity;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 0,compile 'com.google.zxing:core:3.2.1'
 * 1,添加权限
 <!-- 相机权限 -->
 <uses-permission android:name="android.permission.CAMERA" />
 <!-- 震动权限 -->
 <uses-permission android:name="android.permission.VIBRATE" />
 * 2,添加ids.xml文件 && colors.xml中zxing相关部分
 * 3,添加activity_zxing.xml 就是扫描的页面布局
 * 4,CaptureActivity 就是扫描的页面，目前是扫码成功后通过回调返回扫码结果
 * 5,zxing包下的import以及红线错误，根据包名修改
 * 6,zxing默认扫描有效区域为方块区域，大小范围在CameraManager里调整
 * 7,如果需要生成二维码图片可以通过QRCodeUtils完成
 */
public class ZxingDemoActivity extends BaseActivity {

    @Bind(R.id.iv_zxing)
    ImageView ivZxing;

    @Override
    public int getContentViewId() {
        return R.layout.activity_zxing_demo;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        setBack();
        setTitle("zxing");
    }

    @Override
    protected void initVariable() {
        Map<String,Object> map=new HashMap<>();
        map.put("1",new DemoBean(MainActivity.class,"11"));
        DemoBean bean= (DemoBean) map.get("1");
    }

    @Override
    protected void processLogic(Bundle savedInstanceState) {

    }
    @OnClick({R.id.tv_generate_pic, R.id.tv_scan_pic})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_generate_pic:
                Bitmap logo= ImageUtils.drawableToBitmap(getResources().getDrawable(R.mipmap.ic_launcher));
                QRCodeUtils.createImage("中英文混合二维码zxing内容", 300, 300, logo, new QRCodeUtils.QRCodeCreateListener() {
                    @Override
                    public void onCreateSuccess(Bitmap bitmap) {
                        ivZxing.setImageBitmap(bitmap);
                    }
                });

                break;
            case R.id.tv_scan_pic:
                Intent openCameraIntent = new Intent(context,CaptureActivity.class);
                startActivityForResult(openCameraIntent, 0);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data!=null){
            String scanResult = data.getStringExtra("result");
            ToastUtil.showToast(scanResult+"\n扫码慢是因为freeline的关系");
            Bitmap logo=ImageUtils.drawableToBitmap(getResources().getDrawable(R.mipmap.ic_launcher));
            QRCodeUtils.createImage(scanResult,300,300, logo,  new QRCodeUtils.QRCodeCreateListener() {
                @Override
                public void onCreateSuccess(Bitmap bitmap) {
                    ivZxing.setImageBitmap(bitmap);
                }
            });
        }

    }
}
